#pragma once
#include <cstring>
#include <iostream>
#include <time.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#define SIZE 1024
#include <string>
#define FILE_NAME "log.txt"
#define INFO 0
#define DEBUG 1
#define WARNING 2
#define ERROR 3
#define FATAL 4
#define SCREEN 1
#define ONEFILE 2
#define CLASSFILE 3
class Log
{
    public:
    Log()
    {
        printmethod=SCREEN;//默认向显示器打印
        path="./log/";
    }
    void Enable(int method)
    {
        printmethod=method;
        
    }
//日志功能的说明跟实现
std::string leveltoString(int level)
{
    switch(level)
    {
        case 0:return "INFO";
        case 1:return "DEBUG";
        case 2:return "WARNING";
        case 3:return "ERROR";
        case 4:return "FATAL";
        default:
        return "None";
    }
}
void onefilePrint(const std::string&filename,const std::string&logtxt)
{
    std::string Filename=path+filename;
    int fd=open(Filename.c_str(),O_WRONLY|O_CREAT|O_APPEND,0666);
    write(fd,logtxt.c_str(),logtxt.size());
    close(fd);
}
void classfilePrint(int level,const std::string&logtxt)//通过日志等级来进行分类打
{
    //分类本质：创建不同后缀的文件
    std::string filename=FILE_NAME;
    filename+='.';
    filename+=leveltoString(level);
    onefilePrint(filename,logtxt);

}
void logprint(int level,const std::string&logtxt)
{
    switch(printmethod)
    {
        case SCREEN:
        std::cout<<logtxt<<std::endl;//向屏幕打印
        break;
        case ONEFILE:
        onefilePrint(FILE_NAME,logtxt);//向一个文件打印
        break;
        case CLASSFILE:
        classfilePrint(level,logtxt);//分类打印
        break;
    }
}
void operator()(int level,const char*format,...)
{
 char leftbuffer[SIZE]={0};//[年月日][时分秒]
    time_t t=time(nullptr);//获取时间戳
    struct tm* ctime=localtime(&t);//通过时间戳获取想要的时间
    snprintf(leftbuffer,sizeof(leftbuffer),"[%s][%d-%d-%d %d:%d:%d]",leveltoString(level).c_str(),
    ctime->tm_year+1900,ctime->tm_mon+1,ctime->tm_mday,
    ctime->tm_hour,ctime->tm_min,ctime->tm_sec);
    //自定义部分
    char rightbuffer[SIZE]={0};
    va_list s;//char*
    va_start(s,format);//让s指向第一个可变参数

    vsnprintf(rightbuffer,sizeof(leftbuffer),format,s);
    va_end(s);//s=NULL
    //拼接
    char logtxt[SIZE*2]={0};
    snprintf(logtxt,sizeof(logtxt),"%s %s",leftbuffer,rightbuffer);

    logprint(level,logtxt);   
}
// void logmessage(int level,const char*format,...)
// {
//     char leftbuffer[SIZE]={0};//[年月日][时分秒]
//     time_t t=time(nullptr);//获取时间戳
//     struct tm* ctime=localtime(&t);//通过时间戳获取想要的时间
//     snprintf(leftbuffer,sizeof(leftbuffer),"[%s][%d-%d-%d %d:%d:%d]",leveltoString(level).c_str(),
//     ctime->tm_year+1900,ctime->tm_mon+1,ctime->tm_mday,
//     ctime->tm_hour,ctime->tm_min,ctime->tm_sec);
//     //自定义部分
//     char rightbuffer[SIZE]={0};
//     va_list s;//char*
//     va_start(s,format);//让s指向第一个可变参数

//     vsnprintf(rightbuffer,sizeof(leftbuffer),format,s);
//     va_end(s);//s=NULL
//     //拼接
//     char logtxt[SIZE*2]={0};
//     snprintf(logtxt,sizeof(logtxt),"%s %s\n",leftbuffer,rightbuffer);

//     logprint(level,logtxt);
//     }
    ~Log()
    {}
    private:
    int printmethod;//打印方式
    std::string path;
};
Log lg;
